const removeProducts = (arr) => arr.map(str => str.slice(3));

function insertAfterSecondElement(matrix,i)  {
  // 遍历二维数组中的每个子数组
  return matrix.map(subArr => {
    const newSub = [...subArr];
    const insertPos = i < newSub.length ? i + 1 : newSub.length;
    newSub.splice(insertPos, 0, '');
    return newSub;
  });
}
function exportToCSV(data, filename = 'data.csv') {
    const csvContent = data.map(row => row.join(',')).join('\n');
    const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
    const link = document.createElement('a');
    const url = URL.createObjectURL(blob);
    
    link.href = url;
    link.download = filename;
    link.click();
    URL.revokeObjectURL(url);
  }

// 获取所有commodity-item-info.left下第三个直接子div的文本列表
//  货号
let commodityInfos = Array.from(document.querySelectorAll('.commodity-item-info.left'))
  .map(el => {
    let divs = Array.from(el.children)
      .filter(child => child.tagName === 'DIV');
    return divs.length > 2 ? divs[2].textContent.trim() : '';
  });

 commodityInfos = removeProducts(commodityInfos);

// 获取所有class为commodity-item-info left的元素
//  商品名
let itemNodes = document.querySelectorAll('.commodity-item-info.left');
let resultList = [];

// 遍历所有匹配的元素
itemNodes.forEach(item => {
  // 在元素内查找第一个div子元素
  let firstDiv = item.querySelector(':scope > div');
  
  if (firstDiv) {
    // 在div中查找第一个span元素
    let firstSpan = firstDiv.querySelector(':scope > span');
    
    if (firstSpan) {
      // 提取文本并存入数组（去除前后空格）
      resultList.push(firstSpan.textContent.trim());
    }
  }
});

let combinedArray = commodityInfos.map((station, index) => [
    station,
    resultList[index]
  ]);


//  品牌
 resultList = Array.from(document.querySelectorAll('.commodity-item-info.left'))
  .map(el => {
    let divs = Array.from(el.children)
      .filter(child => child.tagName === 'DIV');
    return divs.length > 1 ? divs[1].textContent.trim() : '';
  });
  

  resultList = removeProducts(resultList);
  combinedArray.forEach((subArray, index) => subArray.push(resultList[index]));



  // 获取所有class=armyStation的文本列表
//  商家
let armyStations = Array.from(document.querySelectorAll('.armyStation'))
.map(el => el.textContent.trim());

armyStations = removeProducts(armyStations);
combinedArray.forEach((subArray, index) => subArray.push(armyStations[index]));


//  规格
itemNodes = document.querySelectorAll('.commodity-item-info.left');
 resultList = [];

// 遍历所有匹配的元素
itemNodes.forEach(item => {
  // 在元素内查找第四个div子元素
   let fourthDiv = item.querySelector(':scope > div:nth-of-type(4)');
  
  if (fourthDiv) {
    // 在div中查找第一个span元素
    let firstSpan = fourthDiv.querySelector(':scope > span');
    
    if (firstSpan) {
      // 提取文本并存入数组（去除前后空格）
      resultList.push(firstSpan.textContent.trim());
    }
  }
});

combinedArray.forEach((subArray, index) => subArray.push(resultList[index]));

//数量
resultList = Array.from(document.querySelectorAll('.commodity-item-number.left'))
  .map(el => {
    let divs = Array.from(el.children)
      .filter(child => child.tagName === 'SPAN');
    return divs.length > 1 ? divs[1].textContent.trim() : '';
  });
  resultList = resultList.map(item => Number(item));
  combinedArray.forEach((subArray, index) => subArray.push(resultList[index]));


//  商品小计
resultList = Array.from(document.querySelectorAll('.supplier-total-price'))
.map(el => el.textContent.trim());

resultList = resultList.map(item => Number(item));
combinedArray.forEach((subArray, index) => subArray.push(resultList[index]));

//插入空格
combinedArray = insertAfterSecondElement(combinedArray,0);
combinedArray = insertAfterSecondElement(combinedArray,2);
combinedArray = insertAfterSecondElement(combinedArray,5);
combinedArray = insertAfterSecondElement(combinedArray,8);
combinedArray = insertAfterSecondElement(combinedArray,8);

//将users.csv导出，名字可以改，可将"users.csv"中的"users"改为你想要的名字
// 例如：exportToCSV(combinedArray, 'myData.csv');下载的表格文件就是myData.csv
exportToCSV(combinedArray, 'users.csv');




